package com.geologyoa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.geologyoa.domain.Wpfj01;
import com.geologyoa.domain.Xmfj01;
import com.geologyoa.service.SystemManager;
import com.geologyoa.service.SystemManagerImpl;

public class searchXmfj01Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public searchXmfj01Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		SystemManager sys=new SystemManagerImpl();
		Wpfj01 wpfj01=new Wpfj01();
		String xmName = request.getParameter("xmName");//项目名称
		String fjName = request.getParameter("fjName");//附件名称
		String wdType = request.getParameter("wdType");//文档类别0 PW KY KC SJ SG JL JC JG  
		String wdsubType = request.getParameter("wdsubType");//文档子类别  0 1 2 3  为了后续的扩展，目前用不到 20130616
		
		 List<Xmfj01> list=sys.SearchXmfj(xmName,fjName,wdType,wdsubType);
		 Document document=DocumentHelper.createDocument();
			if(list!=null)
			{
				Element fwlistElement =document.addElement("xmfjList");
				fwlistElement.addAttribute("size", (new StringBuilder(String.valueOf(list.size()))).toString());
				Xmfj01 xmfj;
				Element fwElement;
				for(Iterator iterator=list.iterator();iterator.hasNext();)
				{
					xmfj = (Xmfj01)iterator.next();
					if(xmfj.getXmfj0101()==null)
						continue;
					fwElement=fwlistElement.addElement("xmfjInfo");
					
					fwElement.addAttribute("XMFJ0101", xmfj.getXmfj0101().toString());
					fwElement.addAttribute("XMFJ0102", xmfj.getXmfj0102());
					fwElement.addAttribute("XMFJ0103", xmfj.getXmfj0103());
					fwElement.addAttribute("XMFJ0104", xmfj.getXmfj0104());
					fwElement.addAttribute("XMFJ0105", xmfj.getXmfj0105());
					fwElement.addAttribute("XMFJ0106", xmfj.getXmfj0106());
					fwElement.addAttribute("XMFJ0107", xmfj.getXmfj0107());
					
					
				}
			}
			StringWriter xmlout = new StringWriter();
			XMLWriter output = new XMLWriter(xmlout);
			output.write(document);
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("Cache-Control", "no-cache");
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "No-cache");
			response.getWriter().write(xmlout.toString());
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
